package com.gexin.controller.xapp.acitivity;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.MessageUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.interceptor.XAppSafeInterceptor;
import com.gexin.core.web.AppController;
import com.gexin.model.activity.MemberBillSpeedIntegral;
import com.gexin.model.activity.MemberCommisionLog;
import com.gexin.model.weixin.WeixinUser;
import com.gexin.service.memberAcitivty.CoinService;
import com.gexin.service.memberAcitivty.IntegralService;
import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

@Before(XAppSafeInterceptor.class)//统一错误拦截
@ControllerBind(controllerKey = "/xapp/CoinController")
public class CoinController  extends AppController {

    private CoinService coinService = new CoinService();

    /**
     * 可兑换金额
     */
    public void coinExchangeMoneyConvernet(){
        String auth = getAuthUser().getId()+"";
        ApiResponse apiResponse = coinService.coinExchangeMoneyConvernet(auth);
        renderJson(apiResponse);
        return;
    }


    /**
     * 佣金提现
     */
    @Before(Tx.class)
    public void coinExchangeMoney(){
        String money = getPara("balance");
        String openid = getPara("openid");
        if(openid==null){
            ApiResponse apiResponse = new ApiResponse("请确认openid是否正确");
            renderJson(apiResponse);
            return;
        }
        String auth =getAuthUser ().getId()+"";
        ApiResponse apiResponse = coinService.coinExchangeMoney(auth, money,openid);
        renderJson(apiResponse);
        return;
    }


    /**
     * 获得提现记录日志
     */
    public void getMemberCommision(){
        String authUser = getAuthUser().getId()+"";
//        List<CashCouponLog> commision = integralService.getCommision(authUser);
        List<MemberCommisionLog> commision = coinService.getCommision(authUser);
        renderJson(new ApiResponse(commision));
        return;
    }

    //发送验证码
    public void sendVerificationPayCode() {
        String phone = getPara("phone");
        if (StringUtils.isEmpty(phone)) {
            fail("请输入手机号！");
            return;
        }
        Random rand = new Random();
        String code = StringUtil.fillString(rand.nextInt(10000) + "", "0", 4, StringUtil.FILL_TYPE_LEFT);
        renderJson(MessageUtils.SendCode_New(phone, code, MessageUtils.loginId,getRequest()));
        return;
    }

    /**
     * 校验支付密码
     */
    public void getVerifyPassword(){
        String userId = getAuthUserId()+"";
        String password = getPara("payPassword");
        ApiResponse payPassword = coinService.getPayPassword(userId, password);
        renderJson(password);
        return;
    }

    /**
     * 金币赠送
     */
    @Before(Tx.class)
    public void presentCoin(){
        String presentId = getPara("presentId");
        int coin = getParaToInt("coin");
        String authUser = getAuthUser().getId()+"";
        ApiResponse apiResponse = coinService.presentCoin(authUser, presentId, BigDecimal.valueOf( coin));
        renderJson(apiResponse);
        return;
    }


    /**
     * 获得佣金总和
     */
    public void getMemberBillSpeedIntegral(){
        WeixinUser authUser = getAuthUser();
        MemberBillSpeedIntegral memberInfo = MemberBillSpeedIntegral.dao.getMemberBillSpeedIntegralByUserId(authUser.getId() + "");
        ApiResponse apiResponse = new ApiResponse(true, "获取成功", memberInfo);
        renderJson(apiResponse);
        return;
    }



    /**
     * 获取金币总数
     * @return
     */
    public ApiResponse getCoinCount(){
        String weixinUser = getAuthUser().getId()+"";
        IntegralService integralService = new IntegralService();
        ApiResponse apiResponse = integralService.getCoinCount(weixinUser);
        renderJson(apiResponse);
        return apiResponse;
    }

}
